#include <iostream>
using namespace std;

/**
 * 删除头节点
 */
void Test01(){
    int train[5] = {0, 10, 20, 30, 40};
    int next[5] = {1, 3, 4, 2, -1};
    
    int head = 0;

    cout << "节点删除前：\n";

    while(head != -1){
        cout << train[head] << " ";
        head = next[head];
    }

    head = 0;
    int del=0;

    if(head==del){
        head = next[head];
        next[del]=-1;
    }else{
        int cur = head;
        while(next[cur] != del && cur != -1){
            cur = next[cur];
        }
        if(cur != -1){
            next[cur] = next[del];
            next[del]=-1;
        }
    }
    
    cout << "删除头节点后：\n";
    while(head != -1) {
        cout << train[head] << " ";
        head = next[head];
    }
}


/**
 * 删除其他节点
 */
void Test02(){
    int train[5] = {0, 10, 20, 30, 40};
    int next[5] = {1, 3, 4, 2, -1};
    
    int head = 0;

    cout << "节点删除前：\n";

    while(head != -1){
        cout << train[head] << " ";
        head = next[head];
    }

    head = 0;
    int del=3;

    if(head==del){
        head = next[head];
        next[del]=-1;
    }else{
        int cur = head;
        while(next[cur] != del && cur != -1){
            cur = next[cur];
        }
        if(cur != -1){
            next[cur] = next[del];
            next[del]=-1;
        }
    }
    
    cout << "删除节点后：\n";
    while(head != -1) {
        cout << train[head] << " ";
        head = next[head];
    }
}

/**
 * 测试节点删除
 */
int main() {

    // Test01();
    Test02();


    return 0;
}